package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Cate;
import com.ruoyi.system.domain.Delivery;
import com.ruoyi.system.domain.Type;
import com.ruoyi.system.domain.res.projectWeight.WeightDay_res;
import com.ruoyi.system.mapper.CateMapper;
import com.ruoyi.system.mapper.DeliveryMapper;
import com.ruoyi.system.mapper.TypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProjectCheckDetailMapper;
import com.ruoyi.system.domain.ProjectCheckDetail;
import com.ruoyi.system.service.IProjectCheckDetailService;

import javax.annotation.Resource;

/**
 * 竞标项目检查详情Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-22
 */
@Service
public class ProjectCheckDetailServiceImpl implements IProjectCheckDetailService {
    @Resource
    private ProjectCheckDetailMapper projectCheckDetailMapper;
    @Resource
    private TypeMapper typeMapper;
    @Resource
    private CateMapper cateMapper;

    /**
     * 查询竞标项目检查详情
     *
     * @param id 竞标项目检查详情主键
     * @return 竞标项目检查详情
     */
    @Override
    public ProjectCheckDetail selectProjectCheckDetailById(Long id) {
        return projectCheckDetailMapper.selectProjectCheckDetailById(id);
    }

    /**
     * 查询竞标项目检查详情列表
     *
     * @param projectCheckDetail 竞标项目检查详情
     * @return 竞标项目检查详情
     */
    @Override
    public List<ProjectCheckDetail> selectProjectCheckDetailList(ProjectCheckDetail projectCheckDetail) {
        return projectCheckDetailMapper.selectProjectCheckDetailList(projectCheckDetail);
    }

    /**
     * 称重分拣统计
     */
    @Override
    public List<WeightDay_res> selectWeightDay(ProjectCheckDetail projectCheckDetail) {
        //根据日期查询对应配送单关联的所有检查记录
        List<ProjectCheckDetail> checkDetails = projectCheckDetailMapper.selectProjectCheckDetailByWeek(projectCheckDetail.getWeek());
        if (checkDetails == null || checkDetails.isEmpty()) {
            return Collections.emptyList();
        }
        Map<Long, String> cateMap = new HashMap<>();
        checkDetails.forEach(item -> {
            Type type = typeMapper.selectTypeById(item.getTypeId());
            if (type != null) {
                Cate cate = cateMapper.selectCateById(type.getCateId());
                if (cate != null) {
                    item.setCateId(cate.getId());
                    item.setCateName(cate.getName());
                    cateMap.put(item.getCateId(), item.getCateName());
                }
            }
        });
        List<WeightDay_res> resList = new ArrayList<>();
        //根据行业分类id分组
        checkDetails.stream().collect(Collectors.groupingBy(ProjectCheckDetail::getCateId)).forEach((key, value) -> {
            BigDecimal weightGross = BigDecimal.ZERO;
            BigDecimal weightNet = BigDecimal.ZERO;
            BigDecimal weightWaste = BigDecimal.ZERO;
            for (ProjectCheckDetail item : value) {
                if (item.getWeightGross() != null && item.getWeightNet() != null && item.getWeightWaste() != null) {
                    weightGross = weightGross.add(item.getWeightGross());
                    weightNet = weightNet.add(item.getWeightNet());
                    weightWaste = weightWaste.add(item.getWeightWaste());
                }
            }
            BigDecimal weightLoss = BigDecimal.ZERO;
            //计算损耗比
            if (weightGross.compareTo(BigDecimal.ZERO) != 0) {
                weightLoss = weightNet.divide(weightGross, 4, RoundingMode.HALF_UP);
            }
            resList.add(new WeightDay_res(cateMap.get(key), weightGross, weightNet, weightLoss, weightWaste));
        });


        return resList;
    }

    /**
     * 新增竞标项目检查详情
     *
     * @param projectCheckDetail 竞标项目检查详情
     * @return 结果
     */
    @Override
    public int insertProjectCheckDetail(ProjectCheckDetail projectCheckDetail) {
        projectCheckDetail.setCreateTime(DateUtils.getNowDate());
        return projectCheckDetailMapper.insertProjectCheckDetail(projectCheckDetail);
    }

    /**
     * 修改竞标项目检查详情
     *
     * @param projectCheckDetail 竞标项目检查详情
     * @return 结果
     */
    @Override
    public int updateProjectCheckDetail(ProjectCheckDetail projectCheckDetail) {
        projectCheckDetail.setUpdateTime(DateUtils.getNowDate());
        //检查详情修改，毛重和净重不为空，计算损耗比。
        if (projectCheckDetail.getWeightGross() != null && projectCheckDetail.getWeightNet() != null) {
            projectCheckDetail.setWeightLoss(projectCheckDetail.getWeightNet().divide(projectCheckDetail.getWeightNet(), 4, RoundingMode.HALF_UP));
            projectCheckDetail.setWeightWaste(projectCheckDetail.getWeightGross().subtract(projectCheckDetail.getWeightNet()));
        }
        return projectCheckDetailMapper.updateProjectCheckDetail(projectCheckDetail);
    }

    /**
     * 全部通过
     */
    @Override
    public int detailAllToOk(ProjectCheckDetail projectCheckDetail) {
        projectCheckDetail.setUpdateTime(DateUtils.getNowDate());
        return projectCheckDetailMapper.detailAllToOk(projectCheckDetail);
    }

    /**
     * 批量删除竞标项目检查详情
     *
     * @param ids 需要删除的竞标项目检查详情主键
     * @return 结果
     */
    @Override
    public int deleteProjectCheckDetailByIds(Long[] ids) {
        return projectCheckDetailMapper.deleteProjectCheckDetailByIds(ids);
    }

    /**
     * 删除竞标项目检查详情信息
     *
     * @param id 竞标项目检查详情主键
     * @return 结果
     */
    @Override
    public int deleteProjectCheckDetailById(Long id) {
        return projectCheckDetailMapper.deleteProjectCheckDetailById(id);
    }
}
